//
//  WBReturnDefaultButton.m
//  WeiBo
//
//  Created by wbs on 17/2/7.
//  Copyright © 2017年 xiaomaolv. All rights reserved.
//

#import "WBReturnDefaultButton.h"

#define kReturnItemOriginalColor kCSSHexColor(@"#4C4C4C")
#define kReturnItemHighlightColor kCSSHexColor(@"#FF8200")

#define kImageW 16.0f
#define kReturnButtonH 28.0f

@interface WBReturnDefaultButton ()

@end

@implementation WBReturnDefaultButton

+ (WBReturnDefaultButton *)returnDefaultButtonWithTarget:(id)target title:(NSString *)title action:(SEL)action
{
    WBReturnDefaultButton *returnDefaultButton = [[self alloc] init];
    returnDefaultButton.titleLabel.font = kSystemNavigationItemFont;
    CGSize size = [self textSize:title];
    [returnDefaultButton setTitle:title forState:UIControlStateNormal];
    returnDefaultButton.bounds = CGRectMake(0, 0, kImageW + size.width, kReturnButtonH);
    [returnDefaultButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return returnDefaultButton;
}

- (instancetype)init
{
    if (self = [super init]) {
        [self setTitleColor:kReturnItemOriginalColor forState:UIControlStateNormal];
        [self setTitleColor:kReturnItemHighlightColor forState:UIControlStateHighlighted];
        [self setImage:[UIImage imageNamed:@"toolbar_leftarrow"] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"toolbar_leftarrow_highlighted"] forState:UIControlStateHighlighted];
        self.imageView.contentMode = UIViewContentModeCenter;
    }
    return self;
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    return CGRectMake(kImageW, 0, self.width - kImageW, kReturnButtonH);
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    return CGRectMake(0, 0, kImageW, kReturnButtonH);
}

#pragma Private

+ (CGSize)textSize:(NSString *)text
{
    return [text sizeWithAttributes:@{NSFontAttributeName : kSystemNavigationItemFont}];
}

@end
